package rabbitMQ.client;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

public class Publish {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.102");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("ltTestExchange", BuiltinExchangeType.DIRECT, true, false, false, null);

        channel.queueDeclare("ltTestQueue", true, false, false, null);

        channel.queueBind("ltTestQueue", "ltTestExchange", "ltTestRoutingKey");

        String data = "sdfasdfa";

        HashMap<String, Object> header = new HashMap<>();
        header.put("time", "today");
        header.put("local666", "here");
        AMQP.BasicProperties basicProperties = new AMQP.BasicProperties()
                .builder()
                .contentEncoding("utf-8")
                .contentType("text/plain")
                .deliveryMode(2)
                .priority(1)
//                .userId("userId-------")
//                .expiration("6000")
                .headers(header)
                .build();

        channel.confirmSelect();
        channel.basicPublish("ltTestExchange", "ltTestRoutingKey", basicProperties, data.getBytes());
        if (!channel.waitForConfirms()) {
            System.out.println("send message failed");

        } else {
            System.out.println("OK");
        }

        channel.close();
        connection.close();

    }

}
